<?php
/*=============================================================================
|| ##################################################################
||	phpFoX Konsort
|| ##################################################################
||
||	Copyright		: (C) 2005-2007 PHPFOX Limited a Reality Fox Creation
||	Contact			: info@phpfox.com
||
||	- phpFoX and all of its source code and files are protected by Copyright Laws.
||
||	- The license for phpFoX permits you to install this software on a single domain (.com, .org, .net, etc.).
||
||	- You may also not remove this copyright screen which shows the copyright information and credits for phpFoX (RealityFOX Creations).
||
||	- phpFoX Konsort is NOT a FREE software - http://www.phpfox.com/license/
||
|| ##################################################################
=============================================================================*/

/* $File: ./include/classes/payments/payment.class.php, $Author:  $ */

    App::loadClass('DbItem');
    class Payment extends DbItem
    {
        var $sProcessor = '';
        var $oProcessor;
        var $_debug = array();

        function Payment($sProcessor = 'paypal', $sRecive = 'member')
        {
            parent::DbItem();
            $this->sProcessor = $sProcessor;

            switch ($this->sProcessor)
            {
                case 'paypal':
                    App::loadClass('payments/'.$this->sProcessor);
                    $this->oProcessor = &new PayPal($sRecive);
                    break;
                case 'twocheckout':
                    App::loadClass('payments/'.$this->sProcessor);
                    $this->oProcessor = &new TwoCheckout($sRecive, $this->sMode);
                    break;
            }
        }

        /**
         * Make abstract payment
         * @abstract Payment
         *
         * @param integer $iTransactId(sales_id, cheque_id) - all processor
         * @param floatval $fAmount
         * @param mixed $mCustom - custom param (for Metacharge string - Description payment e.g. 'Pay to E-book'
         *                                       for PayPal array (aMemberInfo) e.g.
         *                                          aMemberInfo = array('account_id'=>1,state_id=>31,paddress=>member@email.com)
         *                                          if Recive -> member - aMemberInfo
         *                                          if Recive -> admin - aAdminInfo)
         */
        function makePayment($iTransactId = 0, $fAmount = 0, $mCustom)
        {
            list($sRequest, $sLog) = $this->oProcessor->makePayment(func_get_args());
            $this->logPayment('request', $sLog, $this->sProcessor);
            echo $sRequest;
        }

        function finishPayment()
        {
            $aArg = func_get_args();

            if(empty($aArg[1]))
            {
                $this->logPayment('response', $aArg, $this->sProcessor);
			}

            list($iTransactID, $sStatus) = $this->oProcessor->finishPayment($aArg);

            if($iTransactID && $sStatus)
            {
                return array($iTransactID, $sStatus);
			}
            else
            {
                return array();
			}
        }

        function logPayment()
        {
            $aArgs = func_get_args();
            $oPaymentLogItem = &App::getModuleItem('Account', 'PaymentLog');
            /* @var $oPaymentLog PhpFox_Mod_Account_ItemPaymentLog */
            $oPaymentLogItem->aData['remote_ip']   = $_SERVER['REMOTE_ADDR'];
            $oPaymentLogItem->aData['processor']   = $this->sProcessor;
            $oPaymentLogItem->aData['cdate']       = Database::date();

            switch ($aArgs[0])
            {
                case 'request':
                    $oPaymentLogItem->aData['post_data'] = $aArgs[1];
                    $oPaymentLogItem->aData['type']      = 'request';
                    break;
                case 'response':
                    $oPaymentLogItem->aData['post_data'] = var_export($aArgs[1],true);
                    $oPaymentLogItem->aData['type']      = 'response';
                    break;
            }
            $oPaymentLogItem->insert();
            return;
        }

        function doDebug()
        {
        	/*
        	$sErr = print_r($this->_debug, true);
        	$sTempFile = "file/cache/payment.log";
			$sWrite = (file_exists($sTempFile) ? file_get_contents($sTempFile) . "\n" . $sErr : $sErr);
    		$hFile = fopen($sTempFile, "w+");
    		fwrite($hFile, $sWrite);
    		fclose($hFile);
    		*/
        }
    }
?>